package cn.xuewei.controller;

import cn.xuewei.mapper.CommentMapper;
import cn.xuewei.mapper.ContentMapper;
import cn.xuewei.pojo.dto.CommentDTO;
import cn.xuewei.pojo.entity.Comment;
import cn.xuewei.pojo.vo.CommentVO;
import cn.xuewei.pojo.vo.UserInfoVOComment;
import cn.xuewei.response.ResultVO;
import cn.xuewei.response.StatusCode;
import cn.xuewei.security.CustomUserDetails;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/v1/comments/")
public class CommentController {
    @Autowired
    ContentMapper contentMapper;

    @Autowired
    CommentMapper commentMapper;

    @RequestMapping("insert")
    public ResultVO addComment(@RequestBody CommentDTO commentDTO, @AuthenticationPrincipal CustomUserDetails userDetails) {
        contentMapper.updateCommentCountById(commentDTO.getContentId());
        Comment comment = new Comment();
        BeanUtils.copyProperties(commentDTO, comment);
        comment.setUserId(userDetails.getUserid());
        comment.setCreateTime(new Date());
        comment.setPid(0L);
        commentMapper.insert(comment);
        return ResultVO.ok();
    }

    @RequestMapping("{id}/insertReply")
    public ResultVO addReplyComment(@RequestBody CommentDTO commentDTO, @AuthenticationPrincipal CustomUserDetails userDetails,@PathVariable Long id) {
        contentMapper.updateCommentCountById(commentDTO.getContentId());
        Comment comment = new Comment();
        BeanUtils.copyProperties(commentDTO, comment);
        comment.setUserId(userDetails.getUserid());
        comment.setCreateTime(new Date());
        comment.setPid(id);
        commentMapper.insert(comment);
        return ResultVO.ok();
    }

    @GetMapping("{id}/listComment")
    public ResultVO getAllComments(@PathVariable Long id) {
        List<CommentVO> rootComments = commentMapper.findByContentId(id);//获取所有的父评论
        rootComments.forEach(this::setChildren);
        return ResultVO.ok(rootComments);
    }

    public void setChildren(CommentVO commentVO) {
        List<CommentVO> children = commentMapper.findByParentId(commentVO.getId());
        if (!children.isEmpty()) {
            commentVO.setChildren(children);
            children.forEach(this::setChildren);
        }
    }

    @PostMapping("{id}")
    public ResultVO delete(@PathVariable Long id,@AuthenticationPrincipal CustomUserDetails userDetails){
        // 根据评论id查询发布该评论的用户id
        UserInfoVOComment userInfo = commentMapper.getUserInfoByCommentId(id);
        if (userDetails.getIsAdmin() == 1 || userDetails.getUserid() == userInfo.getUserId()) {
            commentMapper.deletePById(id);
            commentMapper.deleteById(id);
            return ResultVO.ok();
        }
        return new ResultVO(StatusCode.FORBIDDEN_ERROR);
    }

}
